from django.urls import path
from book.views import create_book
from book.views import shop
from book.views import register
from book.views import json,OrderView
from book.views import method, response, set_cookie,get_cookie,set_session,get_session,LoginView
from django.urls.converters import register_converter


class MobileConverter:
    # 验证数据是否合格的关键是正则
    regex = R'1[3-8]\d{9}'

    # 验证没有问题的数据，给视图函数
    def to_python(self, value):
        return value

    # 将匹配结果用于反向解析传输时使用
    def to_url(self, value):
        return str(value)


# 2.先注册转化器，再使用 注册方法：导包
'''
from django.urls.converters import register_converter
converter 转化器类
type_name 转换器名字
'''
register_converter(MobileConverter, 'phone')

urlpatterns = [
    path('create/', create_book),
    # <转换器名字:变量名>
    # 转换器会对变量数据进行 正则验证
    path('<int:city_id>/<phone:mobile>/', shop),
    path('register/', register),
    path('json/', json),
    path('method/', method),
    path('res/', response),
    path('set_cookie/', set_cookie),
    path('get_cookie/', get_cookie),
    path('set_session/', set_session),
    path('get_session/', get_session),
    path('login/', LoginView.as_view()),
    path('overview/', OrderView.as_view()),
]
